package com.universityJob.modules.job.utils;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mao on 2021/4/6.
 * @version 1.0 maozhijob@163.com
 */
@Component
public class SplitListUtils {
    /**
     * List集合拆分
     *
     * @param list  原始数据
     * @param count 每个list的元素数量
     * @param <T>
     * @return 拆分得到的list集合
     */
    public  <T> List<List<T>> splitListByCount(List<T> list, int count) {
        List<List<T>> listAll = new ArrayList<>();
        int size = list.size();
        if (size > count) {
            int absInt = Math.abs(size / count);
            if (size - absInt * count > 0) {
                listAll.add(list.subList(absInt * count, size));
            }
            for (int i = 1; i < absInt + 1; i++) {
                listAll.add(list.subList((i - 1) * count, i * count));
            }
        } else {
            listAll.add(list);
        }
        return listAll;
    }
}
